/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.modules.system.domain.*;
import me.zhengjie.modules.system.mapper.*;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.service.ClazzteachService;
import me.zhengjie.modules.system.domain.vo.ClazzteachQueryCriteria;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author shitou
* @date 2024-12-12
**/
@Service
@RequiredArgsConstructor
public class ClazzteachServiceImpl extends ServiceImpl<ClazzteachMapper, Clazzteach> implements ClazzteachService {

    private final ClazzteachMapper clazzteachMapper;

    private final TeachMapper teachMapper;

    private final TeacherMapper teacherMapper;

    private final ClazzteachteacherMapper clazzteachteacherMapper;

    private final UserMapper userMapper;

    private final StudentMapper studentMapper;

    private final ClazzandstudentMapper clazzandstudentMapper;

    @Override
    public PageResult<Clazzteach> queryAll(ClazzteachQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(clazzteachMapper.findAll(criteria, page));
    }

    public List<Teach> getClazzTeachList(ClazzteachQueryCriteria criteria){
        List<Clazzteach> clazzteachList = clazzteachMapper.findAll(criteria);
        List<Integer> teachIds = new ArrayList<>();
        for(Clazzteach clazzteach:clazzteachList){
            teachIds.add(clazzteach.getTeachId());
        }
        List<Teach> teachList = new ArrayList<>();
        if(teachIds.size()!=0){
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",teachIds);
            if(criteria.getName()!=null){
                queryWrapper.like("name",criteria.getName());
            }
            teachList = teachMapper.selectList(queryWrapper);

            //获取班级教材绑定教师
            QueryWrapper queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("clazzId",criteria.getClazzId());
            queryWrapper1.in("teachId",teachIds);
            List<Clazzteachteacher> clazzteachteacherList = clazzteachteacherMapper.selectList(queryWrapper1);

            List<Integer> teacherIds = new ArrayList<>();
            List<Teacher> teacherList = new ArrayList<>();
            for(Clazzteachteacher clazzteachteacher:clazzteachteacherList){
                teacherIds.add(clazzteachteacher.getTeacherId());
            }
            if(teacherIds.size()!=0){
                QueryWrapper queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.in("id",teacherIds);
                teacherList = teacherMapper.selectList(queryWrapper2);
            }

            //获取教师列表
            for(Teach teach:teachList){
                for(Clazzteach clazzteach:clazzteachList){
                    if(teach.getId() == Math.toIntExact(clazzteach.getTeachId())){
                        teach.setClazzTeachId(clazzteach.getId());
                    }
                }
                List<Teacher> teacherList1 = new ArrayList<>();
                for(Clazzteachteacher clazzteachteacher:clazzteachteacherList){
                    if(clazzteachteacher.getTeachId() == Math.toIntExact(teach.getId())){
                        for (Teacher teacher:teacherList) {
                            if(Math.toIntExact(teacher.getId()) == clazzteachteacher.getTeacherId()){
                                teacherList1.add(teacher);
                            }
                        }
                    }
                }
                teach.setTeacherList(teacherList1);
            }
        }
        return teachList;
    }

    public List<Teach> getStudentClazzTeach(ClazzteachQueryCriteria criteria){

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String currentUserName = authentication.getName();
        User currentUser = userMapper.findByUsername(currentUserName);

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",currentUser.getId());
        Student student = studentMapper.selectOne(queryWrapper);

        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("studentId",student.getId());
        Clazzandstudent clazzandstudent = clazzandstudentMapper.selectOne(queryWrapper1);

        List<Teach> teachList = new ArrayList<>();
        if(clazzandstudent!=null){
            QueryWrapper queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("clazzId",clazzandstudent.getClazzId());
            List<Clazzteach> clazzteachList = clazzteachMapper.selectList(queryWrapper2);

            List<Integer> teachIds = new ArrayList<>();
            for(Clazzteach clazzteach:clazzteachList){
                teachIds.add(clazzteach.getTeachId());
            }
            if(teachIds.size()!=0){
                QueryWrapper queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("id",teachIds);
                //获取课程列表
                teachList = teachMapper.selectList(queryWrapper2);
            }

        }
        return teachList;
    }


    @Override
    public List<Clazzteach> queryAll(ClazzteachQueryCriteria criteria){

        return clazzteachMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Clazzteach resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Clazzteach resources) {
        Clazzteach clazzteach = getById(resources.getId());
        clazzteach.copy(resources);
        saveOrUpdate(clazzteach);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Clazzteach> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Clazzteach clazzteach : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" clazzid",  clazzteach.getClazzId());
            map.put(" teachid",  clazzteach.getTeachId());
            map.put(" lastupdatetime",  clazzteach.getLastupdatetime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}